'use strict';

const db = uniCloud.database();
const collection = db.collection('blogs');
exports.main = async (event, context) => {
	const {
		blogId,
		type
	} = event;

	const res = await collection.doc(blogId).get();
	const likeCount = res.data[0].likeCount;


	if (type === 'increase') {
		if (likeCount) {
			const res2 = await collection.doc(blogId).update({ likeCount: likeCount + 1 });

		} else {
			const res2 = await collection.doc(blogId).update({ likeCount: 1 });
		}
	} else if (type === 'decrease') {
		const res2 = await collection.doc(blogId).update({ likeCount: likeCount - 1 });
	} else {
		return {
			code: 400,
			msg: 'type参数错误'
		};
	}

	//返回数据给客户端
	return {
		code: 0,
		msg: '成功'
	};
};